package com.example.bigevent.controller;

import com.example.bigevent.pojo.Result;
import com.example.bigevent.utils.AliOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
public class UploadController {

    /*
    本地存储
    @PostMapping("/upload")
    public Result upload(String username, Integer age, MultipartFile image) throws IOException {

        // 获取原始文件名, 并提取拓展名
        String originalFilename = image.getOriginalFilename();
        int index = originalFilename.lastIndexOf(".");
        String extname = originalFilename.substring(index);

        // 构造唯一文件名
        String newFilename = UUID.randomUUID().toString() + extname;

        // 输出日志
        log.info("username: {}, age: {}, image: {}", username, age, newFilename);

        // 保存至本地路径
        image.transferTo(new File("D:\\Code\\Java\\SpringWeb\\SpringWeb\\image\\" + newFilename));
        return Result.success();
    }
    */

    @Autowired
    private AliOSSUtils aliOSSUtils;

    // 阿里云 OSS 存储
    @PostMapping("/upload")
    public Result upload(MultipartFile image) throws IOException {
        String url = aliOSSUtils.upload(image);
        return Result.success(url);
    }
}
